/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.tads.ghtads.model;

import br.ufpr.tads.ghtads.controller.Professor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author G0006185
 */

public class GradeDao {
    private final Connection connection;
    private final String sqlinsert ="inseert into grade(horario,diasemana,materia,professor)values(?,?,?,?)";
    private final String sqlmateria = "select p.id,m.id,materiaprincipal from pessoa p inner join Pessoa_Has_Materia pm on p.id=pm.pessoa_id inner join materia m on m.id=pm.materia_id";
    private final String sqlmat = "select * from materia m left join pessoa_has_materia pm on m.id=pm.materia_id";
    public GradeDao() throws IOException, FileNotFoundException, ClassNotFoundException, SQLException {
        this.connection = new ConnectionFactory().getConnection();
    }
     
   
    public void addGrade(GradeDto grade) throws SQLException{
        PreparedStatement stmt = connection.prepareStatement(sqlinsert);
        stmt.setString(1, grade.getHorario());
        stmt.setString(2, grade.getDiasemana());
        stmt.setString(3, grade.getMateria());
        stmt.setString(4, grade.getProfessor());
        stmt.execute();
        stmt.close();
    }
   
    public void gravaEvalGrade() throws IOException, FileNotFoundException, ClassNotFoundException, SQLException, InterruptedException{
        Professor p = new Professor();
        MateriaDao m = new MateriaDao();
        StringBuilder teacher = new StringBuilder();
        StringBuilder materias = new StringBuilder();
        StringBuilder week = new StringBuilder();
         
        
        
        
        for (ProfessorDto professor : p.listProfessor()) {
           teacher.append("'").append(professor.getId()).append(";").append("'").append(",");
           week.append(professor.getSemanaRestricao()).append("%").append(professor.getId()).append("\n");
        }
        for (MateriaDto materia : m.listMateria()) {
            materias.append("'").append(materia.getId()).append(";").append("'").append(",");
        }
        
        String materia = materias.toString().substring(0,materias.length()-1)+"";
        materia = "mat={"+materia+"};";
        
        String teache = teacher.toString().substring(0,teacher.length()-1)+"";
        teache = "prof={"+teache+"};";
        
        String prefweek = week.toString().substring(0,week.length()-4)+"";
        prefweek = "pref=["+prefweek+"];";
        
        
        
        
                
        FileWriter arq = new FileWriter("C:/Users/VictorHugo/Documents/NetBeansProjects/ghtads/src/java/br/ufpr/tads/ghtads/model/eval_grade.m");
        PrintWriter gravarArq = new PrintWriter(arq);
        
        String ar ="function [ output_args ] = eval_grade( input_args )\n";

        ar = ar+teache+"\n";
        ar= ar+materia+"\n";
       
        ar= ar +"% s1, t1, q1, q1, s1, s1, s2, t2, q2, q2, s2, s2\n"; 
        ar = ar+prefweek+"\n";
     

        ar= ar +"%  m1 m2 m3 m4 m5 m6 m7  m8\n";
        ar= ar +"prmt=[ 2, 0, 0, 0, 0, 0, 0, 0;  %prof1\n";
        ar= ar +"   0, 2, 0, 0, 0, 0, 0, 0;  %prof2\n";
        ar= ar +"   0, 0, 2, 0, 0, 0, 0, 0;  %prof3\n";
        ar= ar +"   0, 0, 0, 2, 0, 0, 0, 0;  %prof4\n";
        ar= ar +"   0, 0, 0, 0, 2, 0, 0, 0;  %prof5\n";
        ar= ar +"   0, 0, 0, 0, 0, 2, 0, 2;  %prof6\n";
        ar= ar +"   0, 0, 0, 0, 0, 0, 2, 0]; %prof7\n";
   
        ar= ar +"%     m1 m2  m3  m4  m5  m6  m7  m8\n";
        ar= ar +"matt=[ 1, 2, 2, 1, 2, 1, 2, 1];  %prof1\n";
ar= ar +"%até isso \n";

 ar= ar +"B = bits2bytes(input_args);\n"; 

 ar= ar +"err = [];\n";
 ar= ar +"merr = [];\n";
 ar= ar +"perr = [];\n";
 ar= ar +"terr = [];\n";
 ar= ar +"Bp=B(1:12);\n";
 ar= ar +"gradep=mod(Bp, size(prof,2))+1;\n";
 ar= ar +"err(1)=sum (not(diag(pref(gradep,:))));\n";
 ar= ar +"Bm=B(13:24);\n";
 ar= ar +"gradem=mod(Bm,size(mat,2))+1;\n";
 ar= ar +"merr(1)=prmt(gradep(1),gradem(1))*10000;\n";
 ar= ar +"merr(2)=prmt(gradep(2),gradem(2))*10000;\n";
 ar= ar +"merr(3)=prmt(gradep(3),gradem(3))*10000;\n";
ar= ar +"merr(4)=prmt(gradep(4),gradem(4))*10000;\n";
ar= ar +"merr(5)=prmt(gradep(5),gradem(5))*10000;\n";
ar= ar +"merr(6)=prmt(gradep(6),gradem(6))*10000;\n";
ar= ar +"merr(7)=prmt(gradep(7),gradem(7))*10000;\n";
ar= ar +"merr(8)=prmt(gradep(8),gradem(8))*10000;\n";
ar= ar +"merr(9)=prmt(gradep(9),gradem(9))*10000;\n";
ar= ar +"merr(10)=prmt(gradep(10),gradem(10))*10000;\n";
ar= ar +"merr(11)=prmt(gradep(11),gradem(11))*10000;\n";
ar= ar +"merr(12)=prmt(gradep(11),gradem(12))*10000;\n";

ar= ar +"perr(1)=sum(gradep(find(gradem==1))-gradep(find(gradem==1,1)))+1;\n";
ar= ar +"perr(2)=sum(gradep(find(gradem==2))-gradep(find(gradem==2,1)))+1;\n";
ar= ar +"perr(3)=sum(gradep(find(gradem==3))-gradep(find(gradem==3,1)))+1;\n";
ar= ar +"perr(4)=sum(gradep(find(gradem==4))-gradep(find(gradem==4,1)))+1;\n";
ar= ar +"perr(5)=sum(gradep(find(gradem==5))-gradep(find(gradem==5,1)))+1;\n";
ar= ar +"perr(6)=sum(gradep(find(gradem==6))-gradep(find(gradem==6,1)))+1;\n";
ar= ar +"perr(7)=sum(gradep(find(gradem==7))-gradep(find(gradem==7,1)))+1;\n";
ar= ar +"perr(8)=sum(gradep(find(gradem==8))-gradep(find(gradem==8,1)))+1;\n";
ar= ar +"perr(9)=sum(gradep(find(gradem==9))-gradep(find(gradem==9,1)))+1;\n";
ar= ar +"perr(11)=sum(gradep(find(gradem==11))-gradep(find(gradem==11,1)))+1;\n";
ar= ar +"perr(12)=sum(gradep(find(gradem==11))-gradep(find(gradem==12,1)))+1;\n";

ar= ar +"terr(1)=histc(gradem,gradem(1))-matt(gradem(1));\n";
ar= ar +"terr(2)=histc(gradem,gradem(2))-matt(gradem(2));\n";
ar= ar +"terr(3)=histc(gradem,gradem(3))-matt(gradem(3));\n";
ar= ar +"terr(4)=histc(gradem,gradem(4))-matt(gradem(4));\n";
ar= ar +"terr(5)=histc(gradem,gradem(5))-matt(gradem(5));\n";
ar= ar +"terr(6)=histc(gradem,gradem(6))-matt(gradem(6));\n";
ar= ar +"terr(7)=histc(gradem,gradem(7))-matt(gradem(7));\n";
ar= ar +"terr(8)=histc(gradem,gradem(8))-matt(gradem(8));\n";
ar= ar +"terr(9)=histc(gradem,gradem(9))-matt(gradem(9));\n";
ar= ar +"terr(10)=histc(gradem,gradem(10))-matt(gradem(10));\n";
ar= ar +"terr(11)=histc(gradem,gradem(11))-matt(gradem(11));\n";
ar= ar +"terr(12)=histc(gradem,gradem(12))-matt(gradem(12));\n";





ar= ar +"output_args =err+merr+perr+terr;\n";

ar= ar +"end\n";


            

        gravarArq.append(ar);
        arq.close();
        Runtime r = Runtime.getRuntime();
        r.exec("C:/Program Files/MATLAB/R2014a/bin/matlab.exe - nodisplay - nosplash -nodesktop -r run('C:\\Users\\VictorHugo\\Documents\\NetBeansProjects\\ghtads\\src\\java\\br\\ufpr\\tads\\ghtads\\model\\ex_grade.m')");

        String nome = "C:/Users/VictorHugo/Documents/NetBeansProjects/ghtads/src/java/br/ufpr/tads/ghtads/model/prof.txt";
        File F = new File(nome);
        if(!F.exists())
            Thread.sleep(20 * 1000);
        int counter = 0;
        while(true)
        {
            if(F.exists())
                break;
            Thread.sleep(500);
            System.out.println("Counter: " + ++counter);
        }

        FileReader arqf = new FileReader(F);
        BufferedReader lerArq = new BufferedReader(arqf);
        String linha = lerArq.readLine(); // lê a primeira linha

        while (linha != null) {
            System.out.printf("%s\n", linha);
            String[] parts = linha.split(";");
            linha = lerArq.readLine(); // lê da segunda até a última linha
        }
        
        arq.close();
    }
}
